package com.paomi.client.activity.takeTicket;

import android.app.Activity;
import android.app.Dialog;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.TextView;

import com.hannesdorfmann.mosby3.mvp.MvpBasePresenter;
import com.hannesdorfmann.mosby3.mvp.MvpView;
import com.paomi.client.R;
import com.paomi.client.activity.takeTicket.model.TakeTicketBean;
import com.paomi.client.bean.BaseJSON;
import com.paomi.client.net.Api;
import com.paomi.client.net.BaseCallback;
import com.paomi.client.net.Constants;
import com.paomi.client.utils.ToastUtil;

import java.util.HashMap;
import java.util.Map;

import retrofit2.Call;

/**
 * Created by pc on 2018/11/13
 * Describe:
 * com.paomi.client.activity.takeTicket
 */
public class TakeTicketPresenter extends MvpBasePresenter<MvpView> {
    /**
     * 保存的操作
     *
     * @param activity
     */
    public void saveBtn(String opt, String name, String code, String id, Activity activity) {
        LayoutInflater inflater = LayoutInflater.from(activity);
        View view = inflater.inflate(R.layout.dialog_delete_shaow, null);
        final Dialog dialog = new Dialog(activity, R.style.theme_dialog);
        dialog.setContentView(view);
        dialog.show();
        TextView title = view.findViewById(R.id.title);
        title.setText(activity.getResources().getString(R.string.save_sure));
        TextView msg_tv = view.findViewById(R.id.msg_tv);
        msg_tv.setText(activity.getResources().getString(R.string.save_edit));
        TextView textCancel = view.findViewById(R.id.btn_qd);
        textCancel.setText(activity.getResources().getString(R.string.save_cancel));
        textCancel.setTextColor(activity.getResources().getColor(R.color.text_gray_99));
        textCancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dialog.dismiss();
            }
        });
        TextView textSure = view.findViewById(R.id.btn_success);
        textSure.setText(activity.getResources().getString(R.string.save_sure));
        textSure.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dialog.dismiss();
                getPersonTicket(opt,name,code,id,activity);
            }
        });
    }
    /**
     *增加购票人信息
     * @param opt
     * @param name
     * @param code
     * @param id
     * opt		操作方式 save-添加 update-修改
     * name		姓名
     * code		证件码
     * id		opt为update时必传
     */
    public void getPersonTicket(String opt, String name, String code, String id, Activity activity){
        Map<String,String> maps=new HashMap<>();
        maps.put("opt",opt);
        maps.put("name",name);
        maps.put("code",code);
        maps.put("id",id);
        Api.getInstanceService(Constants.User_URL).addTakePerson(maps).enqueue(new BaseCallback<BaseJSON>() {
            @Override
            public void onResponse(Call<BaseJSON> call, BaseJSON result) {
                TakeTicketBean takeTicketBean=new TakeTicketBean();
                takeTicketBean.setCode(code);
                takeTicketBean.setMobile("");
                takeTicketBean.setName(name);
                Intent intent=new Intent();
                intent.putExtra("takeBean",takeTicketBean);
                activity.setResult(2001,intent);
                activity.finish();
            }
        });
    }
    /**
     * 取票人删除的操作
     *
     * @param activity
     */
    public void deleteBtn(Activity activity, String id) {
        LayoutInflater inflater = LayoutInflater.from(activity);
        View view = inflater.inflate(R.layout.dialog_delete_shaow, null);
        final Dialog dialog = new Dialog(activity, R.style.theme_dialog);
        dialog.setContentView(view);
        dialog.show();
        TextView title = view.findViewById(R.id.title);
        title.setText(activity.getResources().getString(R.string.comment_delete));
        TextView msg_tv = view.findViewById(R.id.msg_tv);
        msg_tv.setText(activity.getResources().getString(R.string.ticket_delete));
        TextView textCancel = view.findViewById(R.id.btn_qd);
        textCancel.setText(activity.getResources().getString(R.string.no_delete));
        textCancel.setTextColor(activity.getResources().getColor(R.color.text_gray_99));
        textCancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dialog.dismiss();
            }
        });
        TextView textSure = view.findViewById(R.id.btn_success);
        textSure.setText(activity.getResources().getString(R.string.comment_delete));
        textSure.setTextColor(activity.getResources().getColor(R.color.color_ff0000));
        textSure.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dialog.dismiss();
                deleteTake(activity,id);
            }
        });
    }
    /**
     * 删除取票人
     */
    public void deleteTake(Activity activity,String id) {
        Api.getInstanceService(Constants.User_URL).addTakeDelete(id).enqueue(new BaseCallback<BaseJSON>() {
            @Override
            public void onResponse(Call<BaseJSON> call, BaseJSON result) {
                ToastUtil.getToastEmail().ToastShow(activity, null, 1, R.layout.toast_view, activity.getResources().getString(R.string.delete_success));
                Intent intent=new Intent();
                activity.setResult(2001,intent);
                activity.finish();
            }
        });
    }
}
